#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from zhineng_conn import *
from config_tools import *
from lib.load_data import *
import time
import math
import sys
import re
import os
reload(sys)
sys.setdefaultencoding('utf-8')


def get_warning_data(date):
    dict_conn = set_dict_conn()
    open_conn = set_open_conn()
    st58_conn = set_58_conn()
    stat_conn = set_stats_conn()
    line_list = []
    ch_line_list = []
    
    zd_items = get_zhiding_stat(date,st58_conn)
    get_role_dict(open_conn)
    get_cate_rate_dict(st58_conn)
    get_local_info(dict_conn)
    get_cate_info(dict_conn)
    #get_pre_channel(stat_conn)
    jz_path = '/work3/early_warning/erarlylog_channel_jingzhun'
    f = open(jz_path)
    i = 0
    j = 0
    
    k = 0
    l = 0
    for line in f.readlines():
        #print line
        item = line.split('\t')
        bid = item[15]
        if bid==None or bid=='' or float(bid) == 0:
            continue
            
        channel_id = item[0]
        channel_name = item[1]
        channel_pv = item[13]
        area_id = item[5]
        cate2_name = item[12]
        cate3_name = item[8]
        if cate3_name == None:
            cate3_name=''
            
        cate_name = cate3_name
        if cate_name == '':
            cate_name = cate2_name
        
        #pindao liuliang jiexian
        jz_ps_pv,jz_ps_user_count,jz_pre_cpc,jz_pre_price = get_warning_config(0,area_id,cate3_name,cate_name,dict_conn,open_conn)
        #print channel_pv
        #print jz_ps_pv,jz_ps_user_count,jz_pre_cpc,jz_pre_price
        if int(jz_ps_pv) == 0 :
            continue

        zj_ps_pv,zd_ps_user_count,zd_pre_cpc,zd_pre_price  = get_warning_config(1,area_id,cate3_name,cate_name,dict_conn,open_conn)
        
        #print "test"
        
        channel_name_city  = item[2]
        channel_name_cate = item[3]
        
        city_id = item[4]        
        cate1_id = item[6]
        cate2_id = item[7]        
        city_name = item[9]
        area_name = item[10]
        cate1_name = item[11]
               
        jz_vip_user_count = item[14]
        
        success_count = item[16]
        jz_user_count = item[17]
        jz_base_price = item[18]
        position_count = item[19]
        bid_max = item[20]
        bid_min = item[21]
        #jing zhun
        jz_pv = item[22]
        jz_click = item[23]
        jz_cpc = item[24]
        if jz_cpc == None or jz_cpc == '' or jz_cpc == '\n':
            jz_cpc = '0'
        if jz_base_price == None or jz_base_price=='':
            jz_base_price = '0'

        match_channel = 0
        for zd in zd_items:
            zd_city_id = zd[0]
            zd_city_name = zd[1]
            zd_area_id = zd[2]
            zd_area_name = zd[3]
            zd_cate1_id = zd[4]
            zd_cate1_name = zd[5]
            zd_cate2_id = zd[6]
            zd_cate2_name = zd[7]
            if zd_city_name == None:
                continue
            zd_show_pv = zd[8]
            zd_user_count = zd[9]
            zd_vip_user_count = zd[10]
            zd_cpc = zd[11]
            
            zd_gs_city_id = zd[12]
            zd_gs_area_id = zd[13]
            zd_gs_cate1_id = zd[14]
            zd_gs_cate2_id = zd[15]
            zd_avg_amount = zd[16]
            zd_clicks = zd[17]
            if zd_cpc == None:
                zd_cpc = 0
            
            zd_channel_name = zd_city_name
            if zd_area_name != None and zd_area_name <> '' :
                zd_channel_name = zd_area_name

            zd_channel_name += "-%s"%zd_cate2_name
            
            #print zd_channel_name,channel_name
            if channel_name != zd_channel_name:
                continue
            
            #pre channel
            if zd_cate1_id != int(cate1_id) and zd_cate2_id != int(cate2_id) :
                #print channel_name,cate1_id,cate1_name,zd_cate2_id,cate2_name,zd_cate1_id,zd_cate1_name,cate2_id,zd_cate2_name
                continue
            match_channel =1
            
            #print channel_name
            zd_hour_price = 0
            try:
                zd_hour_price = get_zhiding_price(zd_gs_city_id,zd_gs_area_id,zd_gs_cate1_id,zd_gs_cate2_id,st58_conn)
            except Exception:
                st58_conn = set_58_conn()
                zd_hour_price = get_zhiding_price(zd_gs_city_id,zd_gs_area_id,zd_gs_cate1_id,zd_gs_cate2_id,st58_conn)
            if zd_hour_price == 0 :
                zd_hour_price = 5
                
            cate_rate = 8.16;
            if cate_rate_dict.has_key(str(zd_gs_cate2_id) ):
                cate_rate = cate_rate_dict[str(zd_gs_cate2_id)]                
            zd_price = zd_hour_price * cate_rate * 0.1
            
            zd_hour_price = zd_hour_price*0.1
            
            #if float(bid) == 0:
            #    reason = "jingzhun bid=0"
            #    ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc  ,reason]
            #    ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
            #    k +=1
            #    break
            
            if float(channel_pv) < float(jz_ps_pv):
                reason = "channel_pv < jz_ps_pv"
                ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc ,reason]
                ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
                k +=1
                break
            
            if jz_base_price < zd_price *0.85:
                reason = "jz_base_price < zd_price x 0.85"
                ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc ,reason]
                ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
                k +=1
                break

            #user_count
            #print channel_name,jz_user_count,jz_ps_user_count,zd_user_count,zd_ps_user_count
            if float(jz_user_count) < float(jz_ps_user_count) and zd_user_count < float(zd_ps_user_count):
                reason = "jz_user_count<jz_ps_user_count and zd_user_count<zd_ps_user_count"
                print 'jz_cpc is :%s'%jz_cpc
                ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc,reason ]
                ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
                k +=1
                break
            
            
            cpc_range = 0
            price_range = 0
            
            try:
                if zd_cpc > 0:
                    cpc_range = abs( float(jz_cpc) / float(zd_cpc)  - 1)
                else:
                    cpc_range = 1
                if zd_price > 0:
                    price_range = abs( float(jz_base_price) / float(zd_price) - 1)
                else:
                    price_range = 1
            except Exception,e:
                reason = e
                ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc,reason ]
                ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
                k +=1
                break

                
            cpc_range = 100* cpc_range
            price_range = 100 * price_range
            
            #print cpc_range,jz_pre_cpc,price_range,jz_pre_price
            if cpc_range < float(jz_pre_cpc) and price_range < float(jz_pre_price):
                reason = " cpc_range<jz_pre_cpc and price_range<jz_pre_price"
                ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc ,reason]
                ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
                k +=1
                break
                
            #print cpc_range,price_range,jz_pre_cpc,jz_pre_price,channel_name
            role_code = ''
            #pdkf=1
            #print jz_user_count,jz_ps_user_count,zd_user_count,zd_ps_user_count
            if float(jz_user_count) >= float(jz_ps_user_count) and zd_user_count >= float(zd_ps_user_count):
                role_code = "pdkf_1"
                #jzcpc
                if float(jz_cpc) < float(zd_cpc) :
                    role_code += "_jzcpc_0"                    
                else:
                    role_code += "_jzcpc_1"
                #dj
                if float(jz_base_price) < float(zd_price):
                    role_code += "_dj_0"
                else:
                    role_code += "_dj_1"
            elif float(jz_user_count) >= float(jz_ps_user_count) and zd_user_count < float(zd_ps_user_count):
                role_code = "jzkf_1"
                #jzcpc
                if float(jz_cpc) < float(zd_cpc) :
                    role_code += "_jzcpc_0"                    
                else:
                    role_code += "_jzcpc_1"
                #dj
                if float(jz_base_price) < float(zd_price):
                    role_code += "_dj_0"
                else:
                    role_code += "_dj_1"
            elif float(jz_user_count) < float(jz_ps_user_count) and zd_user_count >= float(zd_ps_user_count):
                role_code = "zdkf_1"
                #jzcpc
                if float(jz_cpc) < float(zd_cpc) :
                    role_code += "_jzcpc_0"                    
                else:
                    role_code += "_jzcpc_1"
                #dj
                if float(jz_base_price) < float(zd_price):
                    role_code += "_dj_0"
                else:
                    role_code += "_dj_1"
            
            if role_code == '':
                break
            
            #print role_code
            role_str = ''
            if role_dict.has_key(role_code):
                role_str = role_dict[role_code]
            
            if role_str == '' or role_str == None:
                break
            
            role_arr = role_str.split('_')            
            param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,zd_avg_amount,zd_user_count,zd_show_pv,zd_clicks,zd_cpc,role_arr[0],role_arr[1],role_arr[2],role_arr[3] ]
            #print param
            line_list.append('\t'.join(map(lambda i : str(i), param)))
            i +=1
            break
        #print i
        if i> 4:
            #break
            cont = '\n'.join(line_list)
            path = "/work3/opt/analytic/lh_tmp_data/zhineng/prewarning/"
            file_name = "warning_%s_%s.txt"%(date.replace('-',""),j)
            writeTextToFile(path,file_name,"w",cont)    
            #print cont
            load_data(stat_conn,path+file_name,'pre_warning','\t')
            
            j += 1
            i = 0
            line_list = []
        
        if match_channel == 0:
            
            gs_city_id=0
            gs_area_id=0
            gs_cate1_id=0
            gs_cate2_id=0
            
            if cate_dict.has_key(cate2_id):
                cate = cate_dict[cate2_id]
                c_arr = cate.split('_')
                gs_cate1_id = c_arr[0]
                gs_cate2_id = c_arr[1]
            
            if area_id == 0:
                if local_dict.has_key(city_id):
                    city = local_dict[city_id]
                    l_arr = city.split('_')
                    gs_city_id = c_arr[1]
            else:
                if local_dict.has_key(area_id):
                    city = local_dict[area_id]
                    l_arr = city.split('_')
                    gs_city_id = l_arr[0]
                    gs_area_id = l_arr[1]
                
            zd_hour_price = 0
            try:
                zd_hour_price = get_zhiding_price(gs_city_id,gs_area_id,gs_cate1_id,gs_cate2_id,st58_conn)
            except Exception:
                st58_conn = set_58_conn()
                zd_hour_price = get_zhiding_price(gs_city_id,gs_area_id,gs_cate1_id,gs_cate2_id,st58_conn)
            if zd_hour_price == 0 :
                zd_hour_price = 5
                
            cate_rate = 8.16;
            if cate_rate_dict.has_key(str(zd_gs_cate2_id) ):
                cate_rate = cate_rate_dict[str(zd_gs_cate2_id)]                
            zd_price = zd_hour_price * cate_rate * 0.1
            
            #print zd_price
            zd_hour_price = zd_hour_price*0.1
            
            reason = "channel_pv > jz_ps_pv and channel not match"
            #print 'jz_cpc is :%s'%jz_cpc
            ch_param = [date,channel_id,channel_name,channel_pv,jz_vip_user_count,bid,success_count,jz_user_count,jz_base_price,position_count,bid_max,bid_min,jz_pv,jz_click,float(jz_cpc),zd_price,zd_hour_price,0,0,0,0,0,reason ]
            ch_line_list.append('\t'.join(map(lambda i : str(i), ch_param)))
            k +=1
        
        #print k
        if k > 999 :
            ch_cont = '\n'.join(ch_line_list)
            path = "/work3/opt/analytic/lh_tmp_data/zhineng/prewarning/"
            ch_file_name = "channel_warning_%s_%s.txt"%(date.replace('-',""),l)
            writeTextToFile(path,ch_file_name,"w",ch_cont)
            load_data(stat_conn,path+ch_file_name,'pre_channel_warning','\t')
            
            l += 1
            k = 0
            ch_line_list = []
            
    cont = '\n'.join(line_list)
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/prewarning/"
    file_name = "warning_%s_%s.txt"%(date.replace('-',""),j)
    writeTextToFile(path,file_name,"w",cont)    
    #print cont
    load_data(stat_conn,path+file_name,'pre_warning','\t')
    
    ch_cont = '\n'.join(ch_line_list)
    ch_file_name = "channel_warning_%s.txt"%date.replace('-',"")
    writeTextToFile(path,ch_file_name,"w",ch_cont)
    load_data(stat_conn,path+ch_file_name,'pre_channel_warning','\t')
    
    stat_conn.close()
    st58_conn.close()
    dict_conn.close()
    open_conn.close()
    
    
nav_dict = {}
config_dict = {}
role_dict = {}
channel_dict = {}
cate_rate_dict ={}

cate_dict = {}
local_dict = {}
def get_local_info(conn):
    q = "select displocalid,pid,localid from t_displocal"
    items = conn.get_results(q)
    for item in items:
        dlocal_id = item[0]
        pid = item[1]
        l_id = item[2]
        local_dict[str(dlocal_id)] = "%s_%s"%(pid,l_id)
        
def get_cate_info(conn):
    q = "select dispcategoryid,pid,cateid from t_dispcategory "
    items = conn.get_results(q)
    for item in items:
        dcate_id = item[0]
        pid = item[1]
        c_id = item[2]
        cate_dict[str(dcate_id)] = "%s_%s"%(pid,c_id)

def get_cate_rate_dict(conn):
    q = "select cate_id,cate_rate from top_info_catetime "
    items = conn.get_results(q)
    for item in items:
        cate_id = item[0]
        cate_rate = item[1]
        cate_rate_dict[str(cate_id)] = cate_rate

def get_pre_channel(conn):
    q = "select channel_name from pre_channel"
    items = conn.get_results(q)
    for item in items:
        channel_name = item[0]
        channel_dict[channel_name] = channel_name

def get_role_dict(open_conn):
    q = "select role_code,role_title,adjust,reason,color from t_prewarning_role"
    items = open_conn.get_results(q)
    for item in items:
        role_code = item[0]
        role_title = item[1]
        adjust = item[2]
        reason = item[3]
        color = item[4]
        reason = reason.replace('\n','')
        val_str="%s_%s_%s_%s"%(role_title,adjust,reason,color)
        role_dict[role_code] = val_str


def get_nav_dict(dict_conn):
    q = "select cate_name,channel_id from display_cate"
    items = dict_conn.get_results(q)
    for item in items:
        cate_name = item[0]
        channel_id = item[1]
        
        nav_dict[cate_name] = channel_id

        
def get_config_dict(open_conn):
    q = "select channel_type,nav_id,product_id,pv,user_count,cpc,price from t_prewarning_config"
    items = open_conn.get_results(q)
    for item in items:
        channel_type = item[0]
        nav_id = item[1]
        product_id = item[2]
        pv = item[3]
        user_count = item[4]
        cpc = item[5]
        price = item[6]
        
        key_str = '%s_%s_%s'%(channel_type,nav_id,product_id)
        val_str = '%s_%s_%s_%s'%(pv,user_count,cpc,price)
        config_dict[key_str] = val_str
        

def get_warning_config(product_id,area_id,cate3_name,cate_name,dict_conn,open_conn):
    #1000001 hy 1000003 es 1000004 zp
    channel_type = 0
    if area_id > 0: 
        channel_type = 1
    elif cate3_name != '':
        channel_type = 1
        
    nav_id = 0 ;
    #print nav_dict
    if len(nav_dict) == 0:
        #print 'nav_dict ok'
        get_nav_dict(dict_conn)
        #print nav_dict
        
    
    if nav_dict.has_key(cate_name):        
        nav_id = nav_dict[cate_name]
    
    #print nav_id
    if nav_id == 0:
        return 0,0,0,0
    
    if len(config_dict) == 0:
        print 'nav_dict ok'
        get_config_dict(open_conn)
    
    key_str = '%s_%s_%s'%(channel_type,nav_id,product_id)
    if config_dict.has_key(key_str):
        val_str = config_dict[key_str]
        
        #print val_str
        val_arr = val_str.split('_')
        return val_arr[0],val_arr[1],val_arr[2],val_arr[3]
    
    return 0,0,0,0


def get_zhiding_stat(date,conn):
    
    start_date = var_date(date,"-","-",6)
    end_date = var_date(date,"-","+",1)
    #print start_date
    query = "SELECT disp_city_id,disp_city_name,disp_area_id,disp_area_name,disp_cate1_id,disp_cate1_name,disp_cate2_id,disp_cate2_name,sum(show_pv)/7,sum(top_user_count)/7,sum(vip_user_count)/7, sum(avg_amount)/sum(clicks),city_id,area_id,cate1_id,cate2_id,sum(avg_amount)/7,sum(clicks)/7 FROM `top_info_data_stats` where stat_date between '%s' and '%s' group by disp_city_id,disp_area_id,disp_cate1_id,disp_cate2_id"%(start_date,end_date)
    #print query
    return conn.get_results(query)

def get_zhiding_price(city_id,area_id,cate1_id,cate2_id,conn):
    query = "SELECT price_num FROM `top_info_price` where city_id=%s and area_id=%s and local_id=0 and cate1_id=%s and cate2_id=%s"%(city_id,area_id,cate1_id,cate2_id)
    items = conn.get_results(query)
    if len(items) == 0 :
        return 0
    
    return items[0][0]

def main():

   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        get_warning_data( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
    
    #stat_conn = set_stats_conn()
    #path = "/work3/opt/analytic/lh_tmp_data/zhineng/prewarning/"
    #file_name = "warning_20130508.txt"

    #print cont
    #load_data(stat_conn,path+file_name,'pre_warning','\t')
    
    #stat_conn.close()